package com.liferay.controller;

import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.contant.Contants;
import com.liferay.model.Contact;
import com.liferay.model.MailHolder;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.User;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.service.impl.ContactServiceImpl;

@Controller(value = "ContactController")
@RequestMapping("VIEW")
public class ContactController {
	
	private static Logger log = Logger.getLogger(ContactController.class);
	private long contactID = 0;
	private ContactServiceImpl contactServiceImpl;
	private Contact contact;

	@RenderMapping
	public String setModelAndView(RenderRequest request,
			RenderResponse response, Model model) throws NumberFormatException,
			PortalException, SystemException {
		String userId = request.getRemoteUser();
		if (userId != null) {
			List<Role> list = RoleLocalServiceUtil.getUserRoles(Long
					.parseLong(userId));
			String nameRole = list.get(0).getName();
			if (nameRole.equalsIgnoreCase("Administrator")) {
				contactServiceImpl = new ContactServiceImpl();
				List<Contact> listContact = contactServiceImpl.getAllContact(0, 0);
				model.addAttribute("listContact", listContact);
				return "view";
			}
			for(Role r : list){
				if (r.getName().equalsIgnoreCase("role_student")) {
					return "lienhesinhvien";
				}
			}
		}
		return "lienHeKhach";
	}

	@ActionMapping(params = "action=getinformationcontact")
	public void handleActionEditMark(ActionRequest request, ActionResponse response) {
		contactID = ParamUtil.getLong(request,"contactID");
		log.info(" Contact ID:" + contactID); 
		response.setRenderParameter("action", "showinformationcontact");
	}
	
	@RenderMapping(params= "action=showinformationcontact")
	public String handleRenderEditMark(RenderRequest request, RenderResponse response, Model model){
		contactServiceImpl = new ContactServiceImpl();
		List<Contact> listContact = contactServiceImpl.getContactByID(contactID);
		contact = listContact.get(0);
		model.addAttribute("subject",listContact);
		log.info("show succefully");
		return "showinfor";
	}
	
	@RenderMapping(params= "action=replyinformation")
	public String handleRenderMoveReplyPage(RenderRequest request, RenderResponse response, Model model){
		log.info("handleRenderMoveReplyPage succefully");
		return "reply";
	}
	
	
	@ActionMapping(params = "action=replygetparam")
	public void handleActiongetReplyParam(ActionRequest request, ActionResponse response) {
		String title = ParamUtil.getString(request,"title");
		String content = ParamUtil.getString(request,"content");
		
		String smtpServer = "smtp.gmail.com";
		String from = "11130035@st.hcmuaf.edu.vn";
		String to = contact.getEmail();
		String pwd = "lovetuyetphuong";
		
		MailHolder.send(smtpServer, to, from, pwd,
				title, content);
		log.info(" title:" + title + "\n content:"+ content ); 
		response.setRenderParameter("action", "showinformationcontact");
	}
	
	
	@RenderMapping(params = "render=success")
	public String setRenderForSuccess(RenderRequest request,
			RenderResponse response) {
		return "lienHeSuccess";
	}

	@RenderMapping(params = "render=error")
	public String setRenderForError1(RenderRequest request,
			RenderResponse response) {
		return "errorLienHe";
	}

	@ActionMapping(params = "action=lienHeProcess")
	public void lienHeAction(ActionRequest request, ActionResponse response) {

		String fullname = ParamUtil.getString(request, "fullname");
		String email = ParamUtil.getString(request, "email");
		String phone = ParamUtil.getString(request, "phone");
		String subject = ParamUtil.getString(request, "subject");
		String opinion = ParamUtil.getString(request, "opinion");

		String url = "";

		boolean validate = true;

		// new 1 LienHe
		Contact contact = new Contact();
		contact.setFullName(fullname);
		contact.setEmail(email);
		contact.setPhone(phone);
		contact.setTitle(subject);
		contact.setQuestion(opinion);
		contact.setDateCreate(new Date().toString());
		contact.setIsRead(0);

		String smtpServer = "smtp.gmail.com";
		String from = "codocntt@gmail.com";
		String to = "phongthan73@gmail.com";
		String pwd = "canhsat115";
		String subjectMail = "mail liên hệ";
		String body = contact.toString();

		if (validate) {
			String result = contactServiceImpl.saveContac(contact);
			boolean checkSendMail = MailHolder.send(smtpServer, to, from, pwd,
					subjectMail, body);
			if (result.equals(Contants.SUCCESS) && checkSendMail) {
				url = "success";
			} else {
				url = "error";
			}
		}
		response.setRenderParameter("render", url);
	}

	@ActionMapping(params = "action=lienhesinhvienProcess")
	public void lienHeSinhVienAction(ActionRequest request,
			ActionResponse response) throws NumberFormatException,
			PortalException, SystemException {

		String userId = request.getRemoteUser();
		User user = UserLocalServiceUtil.getUserById(Long.parseLong(userId));

		String fullname = user.getFullName();
		String email = user.getEmailAddress();
		String phone = "";
		String subject = ParamUtil.getString(request, "subject");
		String opinion = ParamUtil.getString(request, "opinion");

		String url = "";

		boolean validate = true;

		// new 1 LienHe
		Contact contact = new Contact();
		contact.setFullName(fullname);
		contact.setEmail(email);
		contact.setPhone(phone);
		contact.setTitle(subject);
		contact.setQuestion(opinion);
		contact.setDateCreate(new Date().toString());
		contact.setIsRead(0);

		String smtpServer = "smtp.gmail.com";
		String from = "codocntt@gmail.com";
		String to = "phongthan73@gmail.com";
		String pwd = "canhsat115";
		String subjectMail = "mail liên hệ";
		String body = contact.toString();

		if (validate) {
			String result = contactServiceImpl.saveContac(contact);
			boolean checkSendMail = MailHolder.send(smtpServer, to, from, pwd,
					subjectMail, body);
			if (result.equals(Contants.SUCCESS) && checkSendMail) {
				url = "success";
			} else {
				url = "error";
			}
		}
		response.setRenderParameter("render", url);
	}
	

}